package com.tjetc.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.dao.DepartmentDao;
import com.tjetc.dao.DoctorDao;
import com.tjetc.dao.impl.DepartmentDaoImpl;
import com.tjetc.dao.impl.DoctorDaoImpl;
import com.tjetc.domain.Department;
import com.tjetc.domain.Doctor;
import com.tjetc.service.DoctorService;
import com.tjetc.utils.JedisDb;
import redis.clients.jedis.Jedis;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DoctorServiceImpl implements DoctorService {
    private DoctorDao doctorDao = new DoctorDaoImpl();
    private Jedis jedis = JedisDb.jedis();
    private DepartmentDao departmentDao=new DepartmentDaoImpl();

    @Override
    public Doctor login(String username, String password) {
        try {
            return doctorDao.login(username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Doctor findByName(String username) {
        try {
            return doctorDao.findByName(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Doctor> selectFifth() {
        try {
            return doctorDao.selectFifth();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Doctor> queryByDepid(int depid) {
        try {
            String key = "dep_doctors_"+depid;
            ObjectMapper mapper = new ObjectMapper();
            List<String> depDoctors = jedis.hvals(key);
            List<Doctor> list = new ArrayList<>();
            if (depDoctors!=null&&depDoctors.size()>0){//有数据
                for (String json : depDoctors) {
                    //转json位doctor对象
                    Doctor doctor = mapper.readValue(json, Doctor.class);
                    list.add(doctor);
                }
                return list;
            }else{//无数据，查询数据库
                List<Doctor> doctors = doctorDao.queryByDepid(depid);
                if (doctors!=null&&doctors.size()>0){
                    for (Doctor doctor : doctors) {
                        //存入redis中
                        jedis.hset(key,String.valueOf(doctor.getId()),mapper.writeValueAsString(doctor));
                        jedis.expire(key,60*60L);
                    }
                }else{//缓存空值
                    jedis.hset(key,"","");
                }
                //再次调用此方法
                return queryByDepid(depid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount(String name) {
        try {
            return doctorDao.getCount(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Doctor> list(String name, PageInfo<Doctor> pageInfo) {
        List<Doctor> list= null;
        try {
            list = doctorDao.list(name,pageInfo);
            for (Doctor doctor : list) {
                Department department=departmentDao.getById(doctor.getDepid());
                doctor.setDepartment(department);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Doctor doctor) {
        try {
            //开启事务
            Db.startTransaction();
            //清空redis
            //1.已知添加的医生的depid，查询出来这个科室下的医生
            List<Doctor> list=doctorDao.queryByDepid(doctor.getDepid());
            for (Doctor doctor1 : list) {
                jedis.del("dep_doctors_"+doctor1.getDepid(),String.valueOf(doctor1.getId()));
            }
            //将doctor存入医生表
            doctorDao.add(doctor);
            //将医生所属的小科室人数加1
            Department department=departmentDao.getById(doctor.getDepid());
            department.setStaffs(department.getStaffs()+1);
            departmentDao.addNum(department);

            //将医生所属的大科室人数加1
            Department department1=departmentDao.getById(department.getParent_id());
            department1.setStaffs(department1.getStaffs()+1);
            departmentDao.addNum(department1);
            //提交事务
            Db.commit();
            return  true;

        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            Db.rollback();
        } finally {
            //关闭连接
            Db.close();
        }
        return false;
    }

    @Override
    public boolean update(Doctor doctor) {
        try {
            //清空redis
            //1.已知添加的医生的depid，查询出来这个科室下的医生
            List<Doctor> list=doctorDao.queryByDepid(doctor.getDepid());
            for (Doctor doctor1 : list) {
                jedis.del("dep_doctors_"+doctor1.getDepid(),String.valueOf(doctor1.getId()));
            }
            return doctorDao.update(doctor)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Doctor getById(int id) {
        try {
            return doctorDao.getById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean del(int id) {
        try {
            Db.startTransaction();
            //删除医生表
            doctorDao.del(id);
            //清空redis
            //1.通过id获取到这个医生信息
            Doctor doctor=doctorDao.getById(id);
            // 2.已知添加的医生的depid，查询出来这个科室下的医生
            List<Doctor> list=doctorDao.queryByDepid(doctor.getDepid());
            for (Doctor doctor1 : list) {
                jedis.del("dep_doctors_"+doctor1.getDepid(),String.valueOf(doctor1.getId()));
            }
            //将医生所属的小科室人数减1
            Department department=departmentDao.getById(doctor.getDepid());
            department.setStaffs(department.getStaffs()-1);
            departmentDao.addNum(department);

            //将医生所属的大科室人数减1
            Department department1=departmentDao.getById(department.getParent_id());
            department1.setStaffs(department1.getStaffs()-1);
            departmentDao.addNum(department1);
            //提交事务
            Db.commit();
            return  true;

        } catch (Exception e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }
        return false;
    }

    @Override
    public List<Doctor> listAll() {
        try {
            return doctorDao.listAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getDepidById(int id) {
        try {
            return doctorDao.getDepidById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Doctor getpwdById(int id) {
        try {
            return doctorDao.getpwdById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updatepwd(Doctor doctor) {
        try {
            return doctorDao.updatepwd(doctor)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


}
